/*************************************************
Name : CreateCaseRequest_WS
Created By : dthekumparampil
Created Date : Jan 9, 2013
Description : An Apex REST service class created to recieve the incoming request from WIU for new Case creation.
********************************
Modified By : dthekumparampil
Modified Date : Jan 22, 2013
Modification : 	Changed the Class name from CreateCaseRequest to CreateCaseRequest_WS.
				Added the full logic of the class.
*************************************************/

@RestResource(urlMapping='/CreateCaseRequest/*')

global with sharing class CreateCaseRequest_WS {

/**
Method Name : createCase 
Created By : dthekumparampil
Created Date : Jan 9, 2013 
Description : Its just a dummy method which accepts input and returns success or failure status string.
Input Parameters : String SalesforceID, String Type, String Subject, String Description
Output Parameters : String successOrFailure
********************************************
Modified By : dthekumparampil
Modified Date : Jan 22, 2013
Modification :	Implemented the full Logic.
				First the id is checked in Account, if found, a new Case is created linked to the account with all the information provided in API call.
				If not found in Account, the id is searched in Lead. If found, a new case is created linked to the Lead with all the information provided in API call.
				If not found anywhere, then a Failure message is returned.
				By default the Case priority is kept lowest.

********************************************
Modified By : dthekumparampil
Modified Date : Jan 27, 2013
Modification :	The xml signature has been changed. GroupType, Category, activity have been removed from input.
				The values for grouptype, category and activity are autofilled by trigger.

********************************************
Modified By : dthekumparampil
Modified Date : feb 21, 2013
Modification :	Added Case Origin as 'QuickApp'.
				Now search is based on colleague ID instead of salesforce ID.				
**/    
    @HttpPost
    global static String createCase(String SalesforceID, String CaseName, String Subject, String Description) {
        
        /****Trimming input values ****/
        SalesforceID=SalesforceID.trim();
        CaseName=CaseName.trim();
        Subject=Subject.trim();
        Description=Description.trim();
        
        /****Checking for empty strings in required fields.****/
        /*Modification: GroupType, category and activity have been removed*/
        if(SalesforceID==''|| CaseName==''|| Subject==''||Description==''){
        	return 'Failure: All Fields are mandatory';
        }
        
        /****Getting the RecordTypeIds ****/
        String accountRecordType = Schema.Sobjecttype.Account.getRecordTypeInfosByName().get('West - Student Account').getRecordTypeId();
        String leadRecordType = Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('West - Prospective Student').getRecordTypeId();
        String caseRecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId();
        
        /****Checking ID in Account ****/
        list<Account> accountInSalesforce = [select id, name, Account_Id__C from Account where Account_Id__C=:SalesforceID and RecordTypeId=:accountRecordType];
        
        if(accountInSalesforce.size()>0){
        	try{
	        	Case newCase = new Case(AccountId=accountInSalesforce[0].id,RecordTypeId=caseRecordType,
	        	Case_Name__c=CaseName, Subject=Subject, Description=Description, Manual_status_change__c=true, Origin='QuickApp' );
	        	insert newCase;
	        	return 'Success: '+SalesforceID;
        	}
    		catch(Exception e){
    			system.debug('Account: unable to create case: '+e.getmessage());
    			return 'Failure: Unable to create Case';
    		}
        }
        else{
        	/****Checking in Lead****/
        	list<Lead> leadInSalesforce = [select id, name, Lead_Id__C from Lead where Lead_Id__C=:SalesforceID and RecordTypeId=:leadRecordType];
        	if(leadInSalesforce.size()>0){
        		try{
        			//Case owner gets updated for Cases created  by Web service due to Assignment rules / Support settings. 
        			//Hence we are Using field Manual_status_change__c to trigger the field update which will invoke Case trigger 
        			//and in turn will update the Queue ID for the Case. 
	        		Case newCase = new Case(Lead__c=leadInSalesforce[0].id,RecordTypeId=caseRecordType,
	        		Case_Name__c=CaseName, Subject=Subject, Description=Description, Manual_status_change__c=true, Origin='QuickApp');
	        		insert newCase;
	        		return 'Success: '+SalesforceID;
        		}
        		catch(Exception e){
        			system.debug('Lead: unable to create case: '+e.getmessage());
        			return 'Failure: Unable to create Case';
        		}
        	}
        }
        return 'Failure: Unable to find SalesforceID in database';

        
        /*Old Code*/
        //String successOrFailure = 'Success:001E000000NL4mFIAT';
        //return successOrFailure;
    }
    
    
}